/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: likangnurse_info.c
> Author: lv1
> Website:www.lv1.org
> Created Time: 2024年12月05日 星期四 10时24分17秒
***************************************************/
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
*/
/*
int main(int argc, const char* argv[])
{
	MYSQL mysql;
	mysql_init(&mysql);
	MYSQL*flag;
	flag = mysql_real_connect(&mysql,"localhost","root","1","care",0,NULL,0);
	
	char name[20]={0};
	printf("input name: ");
	scanf("%19s",name);
	
	int age = 0;
	printf("input age: ");
	scanf("%d",&age);
	
	char sex[5] = {0};
	printf("input sex: ");
	scanf("%4s",sex);
	
	char id_number[20] = {0};
	printf("input id_number: ");
	scanf("%19s",id_number);
	
	char family_members[20] = {0};
	printf("input famliy_members: ");
	scanf("%19s",famliy_members);
	
	char phone[15] = {0};
	printf("input phone: ");
	scanf("%14s",phone);
	
	char health_status[20] = {0};
	char pervalence[50]= {0};
	int medicine_fee = 0;
	printf("input health_status: ");
	scanf("%19s",health_status);//健康1，患病2
	if(health_status[0] == '1'){
		pervalence[0] = '\0';
		medicine_fee = 0;
	}else{
		printf("input pervalence: ");
		scanf("%49s",pervalence);
		printf("input medicine_fee: ");
		scanf("%d\n",&medicine_fee);
	}
	
	int month_fee = 2000;
	
	char all[1024];
	sprintf(all,"insert into old_files(name,age,sex,id_number,family_members,phone,health_status,pervalence,medicine_fee,month_fee)\
	values('%s',%d,'%s','%s','%s','%s','%s','%s',%d,%d)",\
	name,age,sex,id_number,family_members,phone,health_status,pervalence,medicine_fee,month_fee);
	if(mysql_query(&mysql, all) != 0) {
        fprintf(stderr, "%s\n", mysql_error(&mysql));
    } else {
        printf("success\n");
    }
	mysql_close(&mysql);
	
    return 0;
}*/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "cJSON.h"
#include <mysql/mysql.h>

#define wip "192.168.0.196"
#define wport 7777

#define rip "192.168.0.196"
#define rport 7777

char *packJson()
{
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	char name[20]={0};
	printf("input name: ");
	scanf("%19s",name);
	cJSON* cjson_name = cJSON_CreateString("name");
	
	int age = 0;
	printf("input age: ");
	scanf("%d",&age);
	cJSON* cjson_age = cJSON_CreateNumber(age);

	char sex[5] = {0};
	printf("input sex: ");
	scanf("%4s",sex);
	cJSON* cjson_sex = cJSON_CreateString("sex");
	
	char id_number[20] = {0};
	printf("input id_number: ");
	scanf("%19s",id_number);
	cJSON* cjson_id_number = cJSON_CreateString("id_number");
	
	char famliy_members[20] = {0};
	printf("input famliy_members: ");
	scanf("%19s",famliy_members);
	cJSON* cjson_famliy_members = cJSON_CreateString("family_members");
	
	char phone[15] = {0};
	printf("input phone: ");
	scanf("%14s",phone);
	cJSON* cjson_phone = cJSON_CreateString("phone");
	
	char health_status[20] = {0};
	char pervalence[50]= {0};
	int medicine_fee = 0;
	printf("input health_status: ");
	scanf("%19s",health_status);//健康1，患病2
	cJSON* cjson_health_status = cJSON_CreateString("health_status");
	if(health_status[0] == '1'){
		//pervalence[0] = '\0';
		cJSON* cjson_pervalence = cJSON_CreateString("");
		//medicine_fee = 0;
		cJSON* cjson_medicine_fee = cJSON_CreateNumber(0);
	}else{
		printf("input pervalence: ");
		scanf("%49s",pervalence);
		cJSON* cjson_pervalence = cJSON_CreateString(pervalence);
		printf("input medicine_fee: ");
		scanf("%d\n",&medicine_fee);
		cJSON* cjson_medicine_fee = cJSON_CreateNumber(medicine_fee);
	}
	
	cJSON* cjson_month_fee = cJSON_CreateNumber(2000);

	cJSON_AddItemToObject(root,"name",cjson_name);
	cJSON_AddItemToObject(root,"age",cjson_age);
	cJSON_AddItemToObject(root,"sex",cjson_sex);
	cJSON_AddItemToObject(root,"id_number",cjson_id_number);
	cJSON_AddItemToObject(root,"phone",cjson_phone);
	cJSON_AddItemToObject(root,"famliy_members",cjson_famliy_members);
	cJSON_AddItemToObject(root,"health_status",cjson_health_status);
	cJSON_AddItemToObject(root,"pervalence",cjson_pervalence);
	cJSON_AddItemToObject(root,"medicine_fee",cjson_medicine_fee);
	cJSON_AddItemToObject(root,"month_fee",cjson_month_fee);

	char *p = (char *)malloc(1024);
	p = cJSON_Print(root);

	printf("%s\n",p);
	cJSON_Delete(root);
	return p;
}

void parseJson(char*p){
	cJSON* root = cJSON_Parse(p);
	cJSON *name = cJSON_GetObjectItem(root,"name");
	cJSON *age = cJSON_GetObjectItem(root,"age");
	cJSON *sex = cJSON_GetObjectItem(root,"sex");
	cJSON *id_number = cJSON_GetObjectItem(root,"id_number");
	cJSON *phone = cJSON_GetObjectItem(root,"phone");
	cJSON *family_members = cJSON_GetObjectItem(root,"family_members");
	cJSON *health_status = cJSON_GetObjectItem(root,"health_status");
	cJSON *pervalence = cJSON_GetObjectItem(root,"pervalence");
	cJSON *medicine_fee = cJSON_GetObjectItem(root,"medicine_fee");
	cJSON *month_fee = cJSON_GetObjectItem(root,"month_fee");
	
	char *name1 = cJSON_GetStringValue(name);
	ssize_t age1 = cJSON_GetNumberValue(age);
	char *sex1 = cJSON_GetStringValue(sex);
	char *id_number1 = cJSON_GetStringValue(id_number);
	char *phone1 = cJSON_Gename1tStringValue(phone);
	char *family_members1 = cJSON_GetStringValue(family_members);
	char *health_status1 = cJSON_GetStringValue(health_staus);
	ssize_t pervalence1 = cJSON_GetNumberValue(pervalence);
	ssize_t medicine_fee1 = cJSON_GetNumberValue(medicine_fee);
	char *month_fee1 = cJSON_GetStringValue(month_fee);
	
	printf("name:%s-age:%d-sex:%s-id_number:%s-phone:%s-family_members:%s\
	-health_status:%s-pervalence:%d-medicine_feel:%d-month_fee:%s",\
	name1,age1,sex1,id_number1,phone1,family_members1,health_status1,pervalence1,medicine_fee1,month_fee1);
}
int main(int argc,const char* argv[])
{

	int fp = socket(AF_INET,SOCK_DGRAM,0);
	
	char *p = packJson();
	char*p = packJson();
	parceJson(p);
	free(p);
	close(fp);
	
    return 0;
}
